package ysq.xyg.web.controller;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.web.model.po.*;
import ysq.xyg.web.model.vo.*;
import ysq.xyg.web.service.*;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 网站pc端
 *
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@RestController
public class PcController {
    private static Logger log= LoggerFactory.getLogger(PcController.class);
    @Resource
    private WebLinkerService webLinkerService;
    @Resource
    private WebNavigationService webNavigationService;
    @Resource
    private WebTagTypeService webTagTypeService;
    @Resource
    private WebBannerService webBannerService;
    @Resource
    private WebColumnTypeService webColumnTypeService;
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private WebArticleService webArticleService;
    @Resource
    private WebImagesService webImagesService;
    @Resource
    private WebProductService webProductService;
    @Resource
    private WebAreaLeaderService webAreaLeaderService;
    @Resource
    private WebCooperaterService webCooperaterService;
    /**
     * 统一首页
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/")
    public ModelAndView s(){
        return new ModelAndView("redirect:/managerLogin");
    }
    /**
     * 统一首页
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/index")
    public ModelAndView index(HttpServletRequest request,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/index");
        modelAndView=setBaseNews(navId,modelAndView);
        //获取产品栏目分类
        List<WebColumnType> list=webColumnTypeService.acceptList(new WebColumnTypeVo(WebBaseContent.web_col_type_cp,BaseConstant.base_show_type_zs,""));
        modelAndView.addObject("webColumnTypeList",list);
        if(list!=null&&list.size()>0){
            modelAndView.addObject("productColFlag",list.get(0).getColFlag());
        }
        //获取新闻栏目分类
        List<WebColumnType> newsList=webColumnTypeService.acceptList(new WebColumnTypeVo(WebBaseContent.web_col_type_ptwz,"","",WebBaseContent.web_col_extend_type_xw));
        modelAndView.addObject("newsList",newsList);
        if(newsList!=null&&newsList.size()>0){
            modelAndView.addObject("newsColFlag",newsList.get(0).getColFlag());
        }
        //banner图
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(WebBaseContent.web_banner_code_sy,BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,""));
        modelAndView.addObject("webBannerList",webBannerList);
        return modelAndView;
    }
    /**
     * 登录
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/login")
    public ModelAndView login(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView("/pc/login");
        modelAndView=setBaseNews("",modelAndView);
        return modelAndView;
    }
    /**
     * 产品市场
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/product")
    public ModelAndView product(HttpServletRequest request,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/product");
        modelAndView=setBaseNews(navId,modelAndView);
        //获取产品栏目分类
        List<WebColumnType> list=webColumnTypeService.acceptList(new WebColumnTypeVo(WebBaseContent.web_col_type_cp,BaseConstant.base_show_type_zs,""));
        modelAndView.addObject("webColumnTypeList",list);
        if(list!=null&&list.size()>0){
            modelAndView.addObject("productColFlag",list.get(0).getColFlag());
        }
        //banner图
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(WebBaseContent.web_banner_code_cp,BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,""));
        modelAndView.addObject("webBannerList",webBannerList);
        return modelAndView;
    }
    /**
     * 渠道合作
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/channel")
    public ModelAndView channel(HttpServletRequest request,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/channel");
        modelAndView=setBaseNews(navId,modelAndView);
        modelAndView.addObject("webAreaLeaderList",webAreaLeaderService.acceptList(new WebAreaLeaderVo()));
        //banner图
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(WebBaseContent.web_banner_code_qd,BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,""));
        modelAndView.addObject("webBannerList",webBannerList);
        return modelAndView;
    }
    /**
     * 博创学院
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/study")
    public ModelAndView study(HttpServletRequest request,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/study");
        modelAndView=setBaseNews(navId,modelAndView);
        return modelAndView;
    }
    /**
     * 帮助中心
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/help")
    public ModelAndView help(HttpServletRequest request,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/help");
        modelAndView=setBaseNews(navId,modelAndView);
        return modelAndView;
    }
    /**
     * 统一详情
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/descTitle")
    public ModelAndView descTitle(HttpServletRequest request,String articleId,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/descTitle");
        modelAndView=setBaseNews(navId,modelAndView);
        WebArticle webArticle=webArticleService.selectByPrimaryKey(articleId);
        modelAndView.addObject("webArticle",webArticle);
        //banner图
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(WebBaseContent.web_banner_code_wz,BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,""));
        modelAndView.addObject("webBannerList",webBannerList);
        return modelAndView;
    }
    /**
     * 统一详情
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/productTitle")
    public ModelAndView productTitle(HttpServletRequest request,String productId,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/productTitle");
        modelAndView=setBaseNews(navId,modelAndView);
        WebProduct webProduct=webProductService.selectByPrimaryKey(productId);
        modelAndView.addObject("webProduct",webProduct);
        //banner图
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(WebBaseContent.web_banner_code_cp,BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,""));
        modelAndView.addObject("webBannerList",webBannerList);
        return modelAndView;
    }

    /**
     * 统一详情
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/desc")
    public ModelAndView desc(HttpServletRequest request,String articleId,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/desc");
        modelAndView=setBaseNews(navId,modelAndView);
        WebArticle webArticle=webArticleService.selectByPrimaryKey(articleId);
        List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(webArticle.getColId(),BaseConstant.base_show_type_zs,""));
        modelAndView.addObject("webArticleList",webArticleList);
        modelAndView.addObject("webArticle",webArticle);
        return modelAndView;
    }
    /**
     * 统一列表
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/list")
    public ModelAndView list(HttpServletRequest request,String colFlag,String navId){
        ModelAndView modelAndView=new ModelAndView("/pc/list");
        modelAndView=setBaseNews(navId,modelAndView);
        modelAndView.addObject("webColumnType",webColumnTypeService.getWebColumnTypeByColFlag(colFlag));
        modelAndView.addObject("colFlag",colFlag);
        //banner图
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(WebBaseContent.web_banner_code_wz,BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,""));
        modelAndView.addObject("webBannerList",webBannerList);
        return modelAndView;
    }

    /**
     * 添加渠道合作意向
     *
     * @param webCooperater 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/web/pc/webCooperaterSave",method = RequestMethod.POST)
    public ReturnEntity webCooperaterSave(WebCooperater webCooperater){
        webCooperater.setId(BaseUtil.uId());
        webCooperater.setCreateTime(new Date());
        webCooperater.setUpdateTime(new Date());
        webCooperater.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        int id = webCooperaterService.insert(webCooperater);
        if(id>0){
            return new ReturnEntity();
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 根据栏目类型获取文章信息
     *
     * @param colFlag 栏目标识
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/web/pc/articlePageByColId",method = RequestMethod.POST)
    public ReturnEntity articlePageByColId(String colFlag, int pageSize, int pageNum){
        ReturnEntity returnEntity = webArticleService.pageByCol(new WebArticleVo(colFlag,BaseConstant.base_show_type_zs),pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 根据标签类型获取文章信息
     *
     * @param tagFlag 标签标识
     * @param colFlag 栏目标识
     * @param maxNum 最多数量
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/web/pc/articleByTagCol",method = RequestMethod.POST)
    public ReturnEntity articleByTagCol(String tagFlag,String colFlag,int maxNum){
        WebTagType webTagType=webTagTypeService.getWebTagTypeByColFlag(tagFlag);
        List<WebArticle> list = webArticleService.listLeftByTagId(new WebArticleVo(BaseConstant.base_show_type_zs,"",colFlag,webTagType.getId()));
        List<WebArticle> resultList=new ArrayList<WebArticle>();
        if(list!=null&&list.size()>0&&maxNum>0&&list.size()>maxNum){
            for(int i=0;i<maxNum;i++){
                if(i<maxNum){
                    resultList.add(list.get(i));
                }else{
                    break;
                }
            }
        }else{
            resultList=list;
        }
        return new ReturnEntity(resultList);
    }
    /**
     * 根据标签类型获取图集信息
     *
     * @param colFlag 栏目标识
     * @param maxNum 最多数量
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/web/pc/imagesByCol",method = RequestMethod.POST)
    public ReturnEntity imagesByCol(String colFlag,int maxNum){
        List<WebImages> list = webImagesService.listLeft(new WebImagesVo(BaseConstant.base_show_type_zs,colFlag));
        List<WebImages> resultList=new ArrayList<WebImages>();
        if(list!=null&&list.size()>0&&maxNum>0&&list.size()>maxNum){
            for(int i=0;i<maxNum;i++){
                if(i<maxNum){
                    resultList.add(list.get(i));
                }else{
                    break;
                }
            }
        }else{
            resultList=list;
        }
        return new ReturnEntity(resultList);
    }

    /**
     * 根据标签类型获取产品信息
     *
     * @param tagFlag 标签标识
     * @param colFlag 栏目标识
     * @param maxNum 最多数量
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/web/pc/productByTagCol",method = RequestMethod.POST)
    public ReturnEntity productByTagCol(String tagFlag,String colFlag,int maxNum){
        WebTagType webTagType=webTagTypeService.getWebTagTypeByColFlag(tagFlag);
        List<WebProduct> list = webProductService.listLeftByTagId(new WebProductVo(BaseConstant.base_show_type_zs,"",colFlag,webTagType.getId()));
        List<WebProduct> resultList=new ArrayList<WebProduct>();
        if(list!=null&&list.size()>0&&maxNum>0&&list.size()>maxNum){
            for(int i=0;i<maxNum;i++){
                if(i<maxNum){
                    resultList.add(list.get(i));
                }else{
                    break;
                }
            }
        }else{
            resultList=list;
        }
        return new ReturnEntity(resultList);
    }
    /**
     * 根据栏目类型获取商品信息
     *
     * @param colFlag 栏目标识
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/web/pc/productPageByColId",method = RequestMethod.POST)
    public ReturnEntity productPageByColId(String colFlag, int pageSize, int pageNum){
        ReturnEntity returnEntity = webProductService.pageByCol(new WebProductVo(colFlag,BaseConstant.base_show_type_zs),pageSize,pageNum);
        return returnEntity;
    }

    public ModelAndView setBaseNews(String navId, ModelAndView modelAndView){
        List<WebNavigation> webNavigationList=webNavigationService.acceptNavMenuList(new WebNavigationVo(BaseConstant.base_use_type_pc));
        List<WebNavigation> currentNavList=new ArrayList<WebNavigation>();
        if(StrUtil.isEmpty(navId)){
            if(webNavigationList!=null&&webNavigationList.size()>0){
                if(StrUtil.isNotEmpty(webNavigationList.get(0).getParentId())){
                    currentNavList.add(webNavigationService.selectByPrimaryKey(webNavigationList.get(0).getParentId()));
                }
                currentNavList.add(webNavigationList.get(0));
                modelAndView.addObject("navId",webNavigationList.get(0).getId());
            }
        }else{
            WebNavigation webNavigation=webNavigationService.selectByPrimaryKey(navId);
            if(webNavigation!=null&&StrUtil.isNotEmpty(webNavigation.getParentId())){
                currentNavList.add(webNavigationService.selectByPrimaryKey(webNavigation.getParentId()));
            }
            currentNavList.add(webNavigation);
            modelAndView.addObject("navId",navId);
        }
        modelAndView.addObject("currentNavList",currentNavList);
        WebConfig webConfig=acceptWebConfig();
        modelAndView.addObject("webConfig",webConfig);

        List<WebColumnType> list=webColumnTypeService.acceptList(new WebColumnTypeVo(WebBaseContent.web_col_type_lxwm));
        List<WebLinker> webLinkerList=webLinkerService.acceptList(new WebLinkerVo());

        if(list!=null&&list.size()>0){
            for(WebColumnType webColumnType:list){
                List<WebLinker> webLinkers=new ArrayList<WebLinker>();
                for(WebLinker webLinker:webLinkerList){
                    if(webColumnType.getId().equals(webLinker.getColId())){
                        webLinkers.add(webLinker);
                    }
                }
                webColumnType.setWebLinkerList(webLinkers);
            }
            modelAndView.addObject("webLinkerList",list);
        }
        modelAndView.addObject("webNavigationList",webNavigationList);
        return modelAndView;
    }

    public WebConfig acceptWebConfig(){
        List<WebConfig> list=webConfigService.acceptList(new WebConfigVo());
        if(list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
